package day20170506.ch01;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理类
 * @author 罗智壕-13424431416
 *
 * 2017年5月6日
 */
public class DynaicProxy implements InvocationHandler{
	
	/**
	 * 目标对象
	 */
	@SuppressWarnings("unused")
	private Object object;
	
	/**
	 * 构造方法
	 */
	public DynaicProxy(Object object){
		this.object = object;
	}
	
	public Object newProxyObject(){
		ClassLoader loader = object.getClass().getClassLoader();
		Class<?>[] interfaces = object.getClass().getInterfaces();
		Object object = Proxy.newProxyInstance(loader, interfaces, this);
		return object;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("进入方法..");
		long begin = System.currentTimeMillis();
		
		Object obj = method.invoke(object, args);
		
		System.out.println("结束方法...");
		long end = System.currentTimeMillis();
		System.out.println("总共花费了" + ((end - begin)/1000) + "秒");
		return obj;
	}
}
